package com.travel.service;

import java.awt.Color;
import java.awt.Font;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import com.travel.core.DaoFactory;
import com.travel.dao.ISpotDao;

public class DrawBar {
	private List<String> years;
	private String SpotName = "桂林市", cityCode;
	private int selectedType = 2;
	private ChartPanel frame1;
	private static final ISpotDao SPOT_DAO = DaoFactory.getDao(ISpotDao.class);

	public DrawBar(String cityCode, String SpotName, List<String> years, int selectedType) {
		this.cityCode = cityCode;
		this.SpotName = SpotName;
		this.selectedType = selectedType;
		this.years = years;
	}

	public JFreeChart feng(CategoryDataset dataset) {
		JFreeChart chart = ChartFactory.createBarChart(SpotName + "游客流量统计", // 图表标题
				"季度", // 目录轴的显示标签
				"游客流量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向：水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
		);
		BarRenderer renderer = new BarRenderer();
		renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setDefaultItemLabelsVisible(true);
		renderer.setDefaultItemLabelPaint(Color.BLACK);
		renderer.setDefaultItemLabelFont(new Font("宋书", Font.PLAIN, 15));

		// 从这里开始
		CategoryPlot plot = chart.getCategoryPlot();// 获取图表区域对象
		CategoryAxis domainAxis = plot.getDomainAxis(); // 水平底部列表
		domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 14)); // 水平底部标题
		domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12)); // 垂直标题
		ValueAxis rangeAxis = plot.getRangeAxis();// 获取柱状
		rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));
		chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));// 设置标题字体
		plot.setRenderer(renderer);// 将修改后的属性值保存到图中
		// 到这里结束，虽然代码有点多，但只为一个目的，解决汉字乱码问题

		frame1 = new ChartPanel(chart, true); // 这里也可以用chartFrame,可以直接生成一个独立的Frame
		return chart;
	}

	public CategoryDataset getDataSet(int n) {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		List<Map<String, Object>> yearCount = SPOT_DAO.getStaticticsByCityOrSpotYear(cityCode, years, selectedType);
		for (String year : years) {
			double value1 = 0;
			double value2 = 0;
			double value3 = 0;
			double value4 = 0;
			int i = 1;
			for (Map<String, Object> ymMap : yearCount) {
				if (ymMap.get("ym").toString().split("-")[0].equals(year)) {
					int val = Integer.parseInt(ymMap.get("count").toString());
					if (i >= 1 && i <= 3) {
						value1 += val;
					}
					if (i >= 4 && i <= 6) {
						value2 += val;
					}
					if (i >= 7 && i <= 9) {
						value3 += val;
					}
					if (i >= 10 && i <= 12) {
						value4 += val;
					}
					i++;
				}
			}
			dataset.addValue(value1, year, "一季度");
			dataset.addValue(value2, year, "二季度");
			dataset.addValue(value3, year, "三季度");
			dataset.addValue(value4, year, "四季度");
			System.out.println(year + " " + value1 + " " + value2 + " " + value3 + " " + value4);
		}
		return dataset;
	}

	public ChartPanel getChartPanel() {
		return frame1;
	}
}
